package com.framework.buss.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.framework.buss.ai.mapper.KnowledgeFileMapper;
import com.framework.buss.ai.model.KnowledgeFile;
import com.framework.buss.ai.params.KnowledgeFileParam;
import com.framework.buss.ai.service.IKnowledgeFileService;
import com.framework.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangzhg
 */
@Service
public class KnowledgeFileService implements IKnowledgeFileService {
    @Autowired
    private KnowledgeFileMapper knowledgeFileMapper;

    @Override
    public List<KnowledgeFile> list(KnowledgeFileParam param) {
        // 查询数据
        QueryWrapper<KnowledgeFile> queryWrapper = new QueryWrapper<>();
        // 前端展示，不同页面的层级不同
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(param.getKnowledgeId()), KnowledgeFile::getKnowledgeId, param.getKnowledgeId())
                .eq(StringUtils.isNotBlank(param.getCategory()), KnowledgeFile::getCategory, param.getCategory())
                .orderByDesc(KnowledgeFile::getCreateTime);
        return knowledgeFileMapper.selectList(queryWrapper);
    }

    @Override
    public KnowledgeFile getOne(String id) {
        return knowledgeFileMapper.selectById(id);
    }

    @Override
    public String save(KnowledgeFile param) {
        String id = param.getId();
        if (StringUtils.isBlank(id)) {
            knowledgeFileMapper.insert(param);
        } else {
            knowledgeFileMapper.updateById(param);
        }

        return param.getId();
    }

    @Override
    public void del(List<String> ids) {
        knowledgeFileMapper.deleteBatchIds(ids);
    }
}
